﻿using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace IceDog.SmallProject.EncryptionAndDecryption
{
    /// <summary>
    /// AES加密解密 .net Core 版本
    /// </summary>
    public class AESCore
    {
        /// <summary>
        /// AES加密文本
        /// </summary>
        /// <param name="text">文本</param>
        /// <param name="key">自定义的秘钥key</param>
        /// <returns></returns>
        public static string AESEncryptText(string text, string key)
        {
            //utf-8编码字节数组
            var encryptKey = Encoding.UTF8.GetBytes(key);
            //创建aes对象
            using (Aes aes = Aes.Create())
            {
                aes.Padding = PaddingMode.PKCS7;
                //创建加密机
                using (var encryptor = aes.CreateEncryptor(encryptKey, aes.IV))
                {
                    //内存流
                    using (var ms = new MemoryStream())
                    {
                        //加密流
                        using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                        {
                            //流写入器
                            using (var sw = new StreamWriter(cs))
                            {
                                sw.Write(text);
                            }
                            //初始向量
                            var iv = aes.IV;

                            //fix iv value
                            //byte[] iv = new byte[] { 233, 5, 144, 72, 5, 253, 125, 153, 75, 225, 132, 191, 127, 186, 153, 87 };
                            //aes.IV = iv;

                            //加密内容字节数组
                            var encryptedContent = ms.ToArray();
                            var result = new byte[iv.Length + encryptedContent.Length];
                            //块拷贝
                            Buffer.BlockCopy(src: iv, srcOffset: 0, dst: result, dstOffset: 0, count: iv.Length);
                            Buffer.BlockCopy(src: encryptedContent, srcOffset: 0, dst: result, dstOffset: iv.Length, count: encryptedContent.Length);
                            //base64编码
                            return Convert.ToBase64String(result);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// AES解密文本
        /// </summary>
        /// <param name="text">文本</param>
        /// <param name="key">自定义的秘钥key</param>
        /// <returns></returns>
        public static string AESDecryptText(string text, string key)
        {
            //base64解码
            var fullCipher = Convert.FromBase64String(text);
            var iv = new byte[16];
            var cipher = new byte[fullCipher.Length - iv.Length];
            //块拷贝
            Buffer.BlockCopy(src: fullCipher, srcOffset: 0, dst: iv, 0, iv.Length);
            Buffer.BlockCopy(src: fullCipher, srcOffset: iv.Length, dst: cipher, 0, fullCipher.Length - iv.Length);
            var decryptKey = Encoding.UTF8.GetBytes(key);
            string result;
            //创建aes对象
            using (Aes aes = Aes.Create())
            {
                //fix iv value
                //aes.IV = iv;
                //创建解密机
                using (var decryptor = aes.CreateDecryptor(decryptKey, iv))
                {
                    //内存流
                    using (var ms = new MemoryStream(cipher))
                    {
                        //加密流
                        using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                        {
                            //流读取器
                            using (var sr = new StreamReader(cs))
                            {
                                result = sr.ReadToEnd();
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}
